﻿using System;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;

namespace Baosight.ZNH.ZMCL.UI
{
    public partial class ZMCLR2 : EFViewCmes
    {
        public ZMCLR2()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            this.LayoutUpdated += new EventHandler(ZMCLR2_LayoutUpdated);
        }

        /// <summary>
        /// 根据主表的状态设置按钮是否可见
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ZMCLR2_LayoutUpdated(object sender, EventArgs e)
        {
            if (this.fdStatus.Value == null)
            {
                return;
            }
            if (this.fdStatus.Value.Equals("生效"))
            {
                Button insert = this.FindName("SELECTRISK") as Button;
                if (insert != null)
                {
                    insert.IsEnabled = false;
                }
                Button delete = this.FindName("DELETERISK") as Button;
                if (delete != null)
                {
                    delete.IsEnabled = false;
                }
            }
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.ChildGrid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 页面初始化操作
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// “选择隐患”按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnSELECTRISKButtonClick(object sender, RoutedEventArgs e)
        {
            Button but = sender as Button;
            but.IsEnabled = false;
            TempButton = but;

            EIInfo eiInfo = new EIInfo();
            EIBlock inquBlock = new EIBlock(EIConstants.QueryStatusBlock);
            EIRow row = new EIRow();
            BindToBlock(inquBlock, "fdType", this.fdType.Value.ToString(), row);
            BindToBlock(inquBlock, "fdRiskDate", this.fdRiskDate.Value.ToString(), row);
            inquBlock.AddRow(row);
            eiInfo.AddBlock(inquBlock);
            HttpServiceAgent agent = new HttpServiceAgent();
            eiInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCLR2");
            eiInfo.Attributes.Add(EIConstants.MethodNameKey, "queryReportNum");
            agent.CallService(eiInfo, OnDoSelectCompleted);
        }

        /// <summary>
        /// 选择隐患的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoSelectCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                EFWindow.Alert(e.EIInfo.GetMsg());
                return;
            }

            if (e.EIInfo.GetBlock(EIConstants.ResultBlock).GetRowCount() == 0)
            {
                base.OpenEFWindowAlert("没有可供选择的当日隐患！", true);
                return;
            }
            SelectRisk zmclSelectRisk = new SelectRisk();
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.Header = "选择隐患记录";
            _popup.ResizeMode = ResizeMode.NoResize;
            _popup.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterScreen;
            zmclSelectRisk.EIDataContext = new Platform.Core.UI.EIDataContext();
            zmclSelectRisk.EIDataContext.Update(e.EIInfo);
            zmclSelectRisk.fdType.Value = this.fdType.Value;
            zmclSelectRisk.fdReportId.Value = this.fdReportId.Value;
            _popup.Content = zmclSelectRisk;
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(Window_Closed);
            _popup.ShowDialog();
            _popup.DialogResult = false;
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.ChildGrid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 绑定值到指定的Block内 
        /// </summary>
        /// <param name="block"></param>
        /// <param name="columnName"></param>
        /// <param name="dataValue"></param>
        private void BindToBlock(EIBlock block, string columnName, object dataValue, EIRow row)
        {
            EIColumn column = new EIColumn();
            column.SetName(columnName);
            row.Add(columnName, dataValue);
            block.AddColumn(column);
        }

        /// <summary>
        /// 查看按钮触发的事件
        /// </summary>
        /// <param name="block"></param>
        /// <param name="columnName"></param>
        /// <param name="dataValue"></param>
        private void ShowRiskButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "隐患项目-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 删除的回调函数
        /// </summary>
        /// <param name="block"></param>
        /// <param name="columnName"></param>
        /// <param name="dataValue"></param>
        private void submitDeleteCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                this.ChildGrid.Refresh();
            }
            else
            {
                EFWindow.Alert(e.EIInfo.GetMsg());
            }
        }

        /// <summary>
        /// 删除按钮触发的事件
        /// </summary>
        /// <param name="block"></param>
        /// <param name="columnName"></param>
        /// <param name="dataValue"></param>
        public void OnDELETERISKButtonClick(object sender, RoutedEventArgs e)
        {
            Button but = sender as Button;
            but.IsEnabled = false;
            TempButton = but;
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.ChildGrid.GetCheckedEIRows();
            base.ViewProperty = null;
            base.ViewProperty = _view;//不能使用OnBeforeAction方法，在这里赋值
            base.OnDeleteButtonClick("删除", "ZMCLR2", "delete", sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            RiskDetail rRiskDetail = new RiskDetail();//构造一个新的修改页面
            
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.riskSource"));//隐患来源
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.riskClassify"));//隐患分类
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.riskLevel"));//隐患级别
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.department"));//隐患单位
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.empName"));//检查人

            rRiskDetail.EIDataContext = new Platform.Core.UI.EIDataContext();
            rRiskDetail.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = rRiskDetail;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(Window_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            //按钮生效
            base.TempButtonOpen(true);
        }
    }
}
